from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from config import CURRENT_CONFIG

# 创建数据库引擎
if "sqlite" in CURRENT_CONFIG.DATABASE_URL:
    engine = create_engine(CURRENT_CONFIG.DATABASE_URL, connect_args={"check_same_thread": False})
else:
    engine = create_engine(CURRENT_CONFIG.DATABASE_URL)


# 创建SessionLocal类
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# 创建数据库依赖函数
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()
